package com.neuedu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.common.ServerResponse;
import com.neuedu.entity.Dept;
import com.neuedu.service.DeptService;
import com.neuedu.vo.DeptCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("科室管理")
@RestController
@RequestMapping("/dept")
@CrossOrigin
public class DeptController {

    @Autowired
    private DeptService deptService;

    @ApiOperation("科室列表")
    @GetMapping("/list")
    public ServerResponse list(DeptCondition deptCondition) {

        System.out.println(deptCondition);

        //调用业务方法
        Page<Dept> deptPage = deptService.getDeptList(deptCondition);

        //返回分页对象
        return ServerResponse.success("").data("deptPage", deptPage);
    }

    @ApiOperation("添加科室")
    @PostMapping("/add")
    public ServerResponse add(@RequestBody Dept dept) {

        System.out.println(dept);

        //服务器端数据校验
        if(StringUtils.isEmpty(dept.getDeptName()))  {
            return ServerResponse.error("科室名称不能为空");
        }

        if(dept.getDeptName().length()>10){
            return ServerResponse.error("科室名称的长度不能超过10个字符");
        }

        if(deptService.save(dept)){
            return ServerResponse.success("添加科室成功");
        }else{
            return ServerResponse.error("添加科室失败");
        }
    }

    @ApiOperation("根据编号获取科室")
    @GetMapping("/get/{deptId}")
    public ServerResponse get(@PathVariable int deptId) {

        Dept dept = deptService.getById(deptId);

        if(dept!=null){
            return ServerResponse.success("").data("dept", dept);
        }else{
            return ServerResponse.error("获取科室信息失败");
        }
    }

    @ApiOperation("修改科室")
    @PutMapping("/modify")
    public ServerResponse modify(@RequestBody Dept dept) {

        System.out.println(dept);

        //服务器端数据校验
        if(StringUtils.isEmpty(dept.getDeptName()))  {
            return ServerResponse.error("科室名称不能为空");
        }

        if(dept.getDeptName().length()>10){
            return ServerResponse.error("科室名称的长度不能超过10个字符");
        }

        if(deptService.updateById(dept)){
            return ServerResponse.success("修改科室成功");
        }else{
            return ServerResponse.error("修改科室失败");
        }
    }

    @ApiOperation("删除科室")
    @DeleteMapping("/remove/{deptId}")
    public ServerResponse remove(@PathVariable int deptId) {

        if(deptService.removeById(deptId)){
            return ServerResponse.success("删除科室成功");
        }else{
            return ServerResponse.error("删除科室失败");
        }
    }

    @ApiOperation("判断指定的科室是否可以删除")
    @GetMapping("/removeEnable/{deptId}")
    public ServerResponse removeEnable(@PathVariable int deptId) {

        if(deptService.removeEnable(deptId)){
            return ServerResponse.success("允许删除科室");
        }else{
            return ServerResponse.error("当前科室下存在医生，不能删除");
        }
    }

    @ApiOperation("批量删除科室")
    @DeleteMapping("/deleteBatch")
    public ServerResponse deleteBatch(String deptIds) {

        if(deptService.deleteBatch(deptIds)){
            return ServerResponse.success("批量删除科室成功");
        }else{
            return ServerResponse.error("批量删除科室成功失败");
        }
    }


    @ApiOperation("根据科室类型获取科室列表")
    @GetMapping("/listAll/{doctorType}")
    public ServerResponse list(@PathVariable int doctorType) {

        List<Dept> deptList = deptService.query().eq("dept_type", doctorType).list();

        return ServerResponse.success("").data("deptList", deptList);
    }
}